Coding2 - Style Intergration
Coding 2: TailwindCSS + ShadCN UI + Path Alias Setupβ
π― Goal:β
Install and configure TailwindCSS and ShadCN UI for reusable components, using the officially recommended Vite + TypeScript method with path aliasing (@
).
π§± A. TailwindCSS Setup (using Vite plugin)β
1. Install Tailwind and the Vite pluginβ
npm install tailwindcss @tailwindcss/vite
2. Update vite.config.ts
β
import path from "path"
import tailwindcss from "@tailwindcss/vite"
import react from "@vitejs/plugin-react"
import { defineConfig } from "vite"
export default defineConfig({
plugins: [react(), tailwindcss()],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
})
3. Create src/styles.css
β
@import "tailwindcss";
4. Import it in main.tsx
β
import "./styles.css"
π§± B. Alias Configuration for TypeScriptβ
1. Update tsconfig.json
β
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
},
"references": [
{ "path": "./tsconfig.node.json" },
{ "path": "./tsconfig.app.json" }
]
}
2. Update tsconfig.app.json
β
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}
3. Update vite.config.ts
β
npm install -D @types/node
import path from "path"
import tailwindcss from "@tailwindcss/vite"
import react from "@vitejs/plugin-react"
import { defineConfig } from "vite"
// https://vite.dev/config/
export default defineConfig({
plugins: [react(), tailwindcss()],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
})
π§± C. ShadCN UI Setupβ
1. Initialize ShadCN UIβ
npx shadcn@latest init
2. Add a ShadCN component (e.g. button)β
npx shadcn@latest add button
β
D. Test ShadCN Button in App.tsx
β
import { Button } from "@/components/ui/button"
export default function App() {
return (
<div className="p-8 text-center space-y-4">
<h1 className="text-3xl font-bold text-blue-500">Tailwind + ShadCN + Vite</h1>
<Button>Click Me</Button>
</div>
)
}
π Resulting Project Snapshotβ
react-crud-app/
βββ src/
β βββ components/ui/button.tsx
β βββ styles.css
β βββ App.tsx
β βββ main.tsx
βββ tsconfig.json
βββ tsconfig.app.json
βββ vite.config.ts
βββ package.json
β Youβre Now Ready for:β
Step 3: React Router Setup (to support pages like Dashboard, Add, Edit, View)
Shall we proceed to the routing setup?